﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using babydealModel;

public partial class itemPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Session["TenDangNhap"] != null) // da dang nhap
            {
                lbNhanXet.Visible = true;
                tbNhanXet.Visible = true;
                btOk.Visible = true;
            }
            else
            {
                lbNhanXet.Visible = false;
                tbNhanXet.Visible = false;
                btOk.Visible = false;
            }
        }
    }   
    protected void ListView1_DataBound(object sender, EventArgs e)
    {                       
        int remain = 0;
        int bought = 0;
        using (babydealEntities context = new babydealEntities())
        {
            int id = Convert.ToInt32(Request.QueryString["id"]);
            mua_hang muaHang = new mua_hang();
            var rsMH = from s in context.mua_hang
                         where s.id_khuyen_mai == id
                         select s;
            khuyen_mai khuyenMai = new khuyen_mai();
            var rsKM = from s in context.khuyen_mai
                         where s.id == id
                         select s.so_luong_toi_da;
            bought = rsMH.Count();
            remain = rsKM.First().Value - bought;
        }
        string a = tbNhanXet.ClientID;
        foreach (ListViewDataItem item in ListView1.Items)
        {
            Label lbHours = (Label)item.FindControl("lbHours");
            Label lbMinutes = (Label)item.FindControl("lbMinutes");
            Label lbSeconds = (Label)item.FindControl("lbSeconds");
            HidHoursLabelID.Value = lbHours.ClientID;
            HidMinutesLabelID.Value = lbMinutes.ClientID;
            HidSecondsLabelID.Value = lbSeconds.ClientID;

            Label lbBought = (Label)item.FindControl("lbBought");
            Label lbRemain = (Label)item.FindControl("lbRemain");
            Label lbCanBuy = (Label)item.FindControl("lbCanBuy");           
            if (remain > 0)
            {
                lbBought.Text = bought.ToString() + " người đã mua";
                lbRemain.Text = "Chỉ còn lại <strong>" + remain.ToString() + "</strong> cơ hội, mua ngay";
                lbCanBuy.Text = "<img src='themes/blue/images/deal-buy-succ.gif'>Vẫn còn mua được...";
            }
            else
            {
                lbBought.Text = bought.ToString() + " người đã mua";
                lbRemain.Text = "<strong>Rất tiếc bạn đã chậm tay</strong>";
                lbCanBuy.Text = "<img src='themes/blue/images/deal-buy-succ.gif'>Không còn hàng rồi T_T";
            }
        }        
    }      
    protected void btOk_Click1(object sender, EventArgs e)
    {
        string comment = tbNhanXet.Text;
        string id_km = Request.QueryString["id"];
        string id_kh;
        if (Session["IDThanhVien"] != null)
        {
            id_kh = Session["IDThanhVien"].ToString();
        }
        else return;
        using (babydealEntities context = new babydealEntities())
        {
            binh_luan obj = new binh_luan();
            obj.id_khuyen_mai = int.Parse(id_km);
            obj.id_thanh_vien = int.Parse(id_kh);
            obj.noi_dung = comment;
            context.AddTobinh_luan(obj);
            context.SaveChanges();
        }
        DataList1.DataBind();
    }
      
    protected void Timer1_Tick(object sender, EventArgs e)
    {
        
        DateTime? end;
        using (babydealEntities context = new babydealEntities())
        {
            khuyen_mai obj = new khuyen_mai();
            int id = int.Parse(Request.QueryString["id"]);
            var rs = from s in context.khuyen_mai
                     where s.id == id
                     select s;
            end = rs.First().ngay_ket_thuc;
        }
        if (end.HasValue)
        {
            TimeSpan ts= end.Value - DateTime.Now;
            if (ts.Seconds < 0)
            {
                ts = TimeSpan.Zero;
            }                        
            lbHours.Text = ts.Hours.ToString();
            lbMinutes.Text = ts.Minutes.ToString();
            lbSeconds.Text = ts.Seconds.ToString();
        }                                               
    }
}
